package com.zlsf.conf;

import io.vertx.config.ConfigRetriever;
import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Config {

  private static Config config;

  private JsonObject configObj;


  private Config() {

  }

  public static Config getInstance() {
    if (config == null) {
      config = new Config();
    }
    return config;
  }

  public void loadConfig(Vertx vertx, Handler<AsyncResult<JsonObject>> result) {
    ConfigStoreOptions customStore = new ConfigStoreOptions()
      .setType("file")
      .setFormat("json")
      .setConfig(new JsonObject().put("path", "conf.json"));

    ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(customStore);
    ConfigRetriever retriever = ConfigRetriever.create(vertx, options);
    retriever.getConfig().onComplete(res -> {
      if (res.succeeded()) {
        this.configObj = res.result();
        log.info("加载配置文件成功");
      }
      result.handle(res);
    });
  }

  public JsonObject getConfig() {
    return configObj;
  }
}
